package com.example.androidcaculator.utils;

import java.util.HashMap;
import java.util.Map;

public class Converter {


    //长度转换器
    public String LengthConversion(String input, String unit1, String unit2) {

        if (unit1.equals(unit2))
            return input;
        if (unit1.equals("英寸")) {
            switch (unit2) {
                case "厘米":
                    return String.valueOf(Double.parseDouble(input) * 2.54);
                case "英尺":
                    return String.valueOf(Double.parseDouble(input) * 12);
                case "公里":
                    return String.valueOf(Double.parseDouble(input) * 0.0000254);
                case "米":
                    return String.valueOf(Double.parseDouble(input) * 0.0254);
                case "毫米":
                    return String.valueOf(Double.parseDouble(input) * 25.4);
                case "微米":
                    return String.valueOf(Double.parseDouble(input) * 25400);
                case "纳米":
                    return String.valueOf(Double.parseDouble(input) * 25400000);
                case "码":
                    return String.valueOf(Double.parseDouble(input) * 36);
            }
        }
        if (unit1.equals("英尺")) {
            //将英尺转换成英寸后再转换成其他单位
            input = String.valueOf(Double.parseDouble(input) / 12);
            unit1 = "英寸";
            return LengthConversion(input, unit1, unit2);
        }
        if (unit1.equals("公里")) {
            input = String.valueOf(Double.parseDouble(input) * 1000);
            unit1 = "米";
            return LengthConversion(input, unit1, unit2);
        }
        if (unit1.equals("米")) {
            switch (unit2) {
                case "厘米":
                    return String.valueOf(Double.parseDouble(input) * 100);
                case "英寸":
                    return String.valueOf(Double.parseDouble(input) * 39.37008);
                case "英尺":
                    return String.valueOf(Double.parseDouble(input) * 3.28084);
                case "公里":
                    return String.valueOf(Double.parseDouble(input) / 1000);
                case "毫米":
                    return String.valueOf(Double.parseDouble(input) * 1000);
                case "微米":
                    return String.valueOf(Double.parseDouble(input) * 1000000);
                case "纳米":
                    return String.valueOf(Double.parseDouble(input) * 1000000000);
                case "码":
                    return String.valueOf(Double.parseDouble(input) * 1.093613);
            }
        }
        if (unit1.equals("厘米")) {
            input = String.valueOf(Double.parseDouble(input) / 100);
            unit1 = "米";
            return LengthConversion(input, unit1, unit2);
        }
        if (unit1.equals("毫米")) {
            input = String.valueOf(Double.parseDouble(input) / 1000);
            unit1 = "米";
            return LengthConversion(input, unit1, unit2);
        }
        if (unit1.equals("微米")) {
            input = String.valueOf(Double.parseDouble(input) / 1000000);
            unit1 = "米";
            return LengthConversion(input, unit1, unit2);
        }
        if (unit1.equals("纳米")) {
            input = String.valueOf(Double.parseDouble(input) / 1000000000);
            unit1 = "米";
            return LengthConversion(input, unit1, unit2);
        }
        if (unit1.equals("码")) {
            input = String.valueOf(Double.parseDouble(input) / 36);
            unit1 = "英寸";
            return LengthConversion(input, unit1, unit2);
        }

        return "";
    }

    //体积转换器
    public String VolumeConversion(String input, String unit1, String unit2) {
        if (unit1.equals(unit2))
            return input;
        if (unit1.equals("毫升")) {
            switch (unit2) {
                case "立方厘米":
                    return input;
                case "升":
                    return String.valueOf(Double.parseDouble(input) / 1000);
                case "立方米":
                    return String.valueOf(Double.parseDouble(input) / 1000000);
                case "液盎司（美制）":
                    return String.valueOf(Double.parseDouble(input) * 0.033814);
            }

        }
        if (unit1.equals("升")) {
            input = String.valueOf(Double.parseDouble(input) * 1000);
            unit1 = "毫升";
            return VolumeConversion(input, unit1, unit2);
        }
        if (unit1.equals("立方厘米")) {
            unit1 = "毫升";
            return VolumeConversion(input, unit1, unit2);
        }
        if (unit1.equals("立方米")) {
            input = String.valueOf(Double.parseDouble(input) * 1000000);
            unit1 = "毫升";
            return VolumeConversion(input, unit1, unit2);

        }
        if (unit1.equals("液盎司（美制）")) {
            input = String.valueOf(Double.parseDouble(input) * 29.57353);
            unit1 = "毫升";
            return VolumeConversion(input, unit1, unit2);

        }


        return "";
    }

    public Map<String, String> BaseConversion(String base, String value) {
        Map<String, String> converted = new HashMap<String, String>();
        Integer valueInt;
        String convertedNum = new String();
        switch (base) {
            case "Dec":
                //如果是十进制，则传进来的value只有数字，直接转换成Int类型
                valueInt = Integer.parseInt(value);

                convertedNum = Integer.toHexString(valueInt).toString();
                converted.put("Hex", convertedNum.toUpperCase());

                convertedNum = Integer.toOctalString(valueInt).toString();
                converted.put("Oct", convertedNum);

                convertedNum = Integer.toBinaryString(valueInt).toString();
                converted.put("Bin", convertedNum);
                break;
            case "Hex":
                //如果传进来的是十六进制的数，则可能会出现字母，此时先用Integer的valueOf转换成十进制的Integer类型
                valueInt = Integer.valueOf(value, 16);
                convertedNum = valueInt.toString();
                converted.put("Dec", convertedNum);

                convertedNum = Integer.toOctalString(valueInt).toString();
                converted.put("Oct", convertedNum);

                convertedNum = Integer.toBinaryString(valueInt).toString();
                converted.put("Bin", convertedNum);
                break;
            case "Oct":
                //同上
                valueInt = Integer.valueOf(value, 8);
                convertedNum = valueInt.toString();
                converted.put("Dec", convertedNum);

                convertedNum = Integer.toHexString(valueInt).toString();
                converted.put("Hex", convertedNum.toUpperCase());

                convertedNum = Integer.toBinaryString(valueInt).toString();
                converted.put("Bin", convertedNum);
                break;
            case "Bin":
                valueInt = Integer.valueOf(value, 2);
                convertedNum = valueInt.toString();
                converted.put("Dec", convertedNum);

                convertedNum = Integer.toHexString(valueInt).toString();
                converted.put("Hex", convertedNum.toUpperCase());

                convertedNum = Integer.toOctalString(valueInt).toString();
                converted.put("Oct", convertedNum);
                break;


        }

        return converted;
    }


}
